﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Globalization;

namespace BSC_OTC_Management.Forms.SoTraiChu
{
    public partial class Frm_SoTraiChu : Form
    {
        #region Khai bao bien
        DataTable _tableData = new DataTable();
        Code.CL_Ultility _cl = new Code.CL_Ultility();
        Hashtable _listKH = new Hashtable();
        Hashtable _listTP = new Hashtable();
        #endregion

        public Frm_SoTraiChu()
        {
            InitializeComponent();
        }

        private void Frm_SoTraiChu_Load(object sender, EventArgs e)
        {
            LoadData();
            LoadDataGrid();
        }

        private void LoadDataGrid()
        {
            this.gridControl1.DataSource = this._tableData;
        }

        private void LoadData()
        {
            // Load du lieu vao table
            this._tableData = _cl._services.Get_Table_SoTraiChu();

            // load combobox KH
            this._listKH.Clear();
            this.cbbMaKH.Items.Clear();
            DataTable tableKH = _cl._services.GetDatatable_BySQL("select * from bsc_tbl_khachhang");
            foreach (DataRow row in tableKH.Rows)
            {
                this._listKH.Add(row["MAKH"].ToString(), row["HOTEN"].ToString());
                this.cbbMaKH.Items.Add(row["HOTEN"].ToString());
            }

            // load combobox TP
            this._listTP.Clear();
            this.cbbMaTPPH.Items.Clear();
            DataTable tableTP = _cl._services.GetDatatable_BySQL("select * from bsc_tbl_tpph");
            foreach (DataRow row in tableTP.Rows)
            {
                this._listTP.Add(row["MATP"].ToString(), row["tentp"].ToString());
                this.cbbMaTPPH.Items.Add(row["tentp"].ToString());
            }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            
            // tao bang chua row can insert
            DataTable tableRow = new DataTable("BSC_TBL_SOTRAICHU");
            
            // tao cac cot 
            tableRow.Columns.Add("MAKH");
            tableRow.Columns.Add("MATP");
            tableRow.Columns.Add("MASO");
            tableRow.Columns.Add("DOTGOPVON");
            tableRow.Columns.Add("NGAYGOPVON");
            tableRow.Columns.Add("SLPHOTHONG");
            tableRow.Columns.Add("SLHANCHE");
            tableRow.Columns.Add("SLCAMCO");

            // tao 1 row chua du lieu
            DataRow rowData = tableRow.NewRow();
            foreach (DictionaryEntry item in this._listKH)
            {
                if (this.cbbMaKH.Text == item.Value.ToString())
                {
                    rowData["MAKH"] = item.Key;
                }
            }
            foreach (DictionaryEntry item in this._listTP)
            {
                if (this.cbbMaTPPH.Text == item.Value.ToString())
                {
                    rowData["MATP"] = item.Key;
                }
            }
            string ngaygopvon =  DateTime.ParseExact(this.dptNgayGopVon.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy");
            rowData["MASO"] = this.txtMaSo.Text;
            rowData["DOTGOPVON"] = this.txtDotGopVon.Text;
            rowData["NGAYGOPVON"] = ngaygopvon;
            rowData["SLPHOTHONG"] = this.txtSLPT.Text;
            rowData["SLHANCHE"] = this.txtSLHC.Text;
            rowData["SLCAMCO"] = this.txtSLCC.Text;
            tableRow.Rows.Add(rowData);

            // tao 1 row chua kieu du lieu
            DataRow rowDataType = tableRow.NewRow();
            rowDataType["MAKH"] = "T";
            rowDataType["MATP"] = "T";
            rowDataType["MASO"] = "T";
            rowDataType["DOTGOPVON"] = "T";
            rowDataType["NGAYGOPVON"] = "D";
            rowDataType["SLPHOTHONG"] = "N";
            rowDataType["SLHANCHE"] = "N";
            rowDataType["SLCAMCO"] = "N";
            tableRow.Rows.Add(rowDataType);

            MessageBox.Show(_cl._services.AddNewRow("BSC_TBL_SOTRAICHU", tableRow));
            LoadData();
            LoadDataGrid();
        }
    }
}
